import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-18
 * Time: 10:56
 */
class Solution6 {
    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer,Integer> hashMap = new HashMap<Integer,Integer>();
        hashMap.put(0 % k , 1);

        int ret = 0 , sum = 0;
        for(int x : nums){
            sum += x;
            int tem = sum % k;//(sum % k + k)%k;//存储余数结果
            ret += hashMap.getOrDefault(tem,0);
            hashMap.put(tem,hashMap.getOrDefault(tem,0)+1);//使用完的余数放进去
        }
        return ret;
    }
}
public class Test6 {
    public static void main(String[] args) {
        Solution6 solution6 = new Solution6();
        int[] nums = {-1,2,9};
        int result = solution6.subarraysDivByK(nums,2);
        System.out.println(result);

    }
}
